package com.springcloud.controller;

import com.springcloud.pojo.Dept;
import com.springcloud.service.DeptClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @ProjectName: springcloud_1
 * @Package: com.com.springcloud.controller
 * @ClassName: DeptConsumerController
 * @Author: 郑涛
 * @Description: feign  社区版 主要是面向接口编程，没有太多的配置
 * @Date: 2020/10/13 10:28
 * @Version: 1.0
 */

@RestController

public class DeptConsumerController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    DeptClientService deptClientService=null;
//    private static final String EEST_URL_DEFALUT = "http://localhost:8005";   //消费者直接调用

    //Ribbon 这里的地址 应该是一个变量，通过服务名来访问
//    private static final String EEST_URL_DEFALUT ="http://SPRINGCLOUD-PROVIDER-DEPT";   //当用ribbno的时候 需要通过微服务的名字去访问

    @PostMapping("/consumer/dept/add")
    public boolean addDept(Dept dept) {
//        return restTemplate.postForObject(EEST_URL_DEFALUT + "/dept/add", dept, Boolean.class);
        return this.deptClientService.addDept(dept);
    }

    @GetMapping("/consumer/dept/QueryAll")
    public List<Dept> QueryAll() {
//        return restTemplate.getForObject(EEST_URL_DEFALUT + "/dept/QueryAll", List.class);
        return this.deptClientService.QueryAll();
    }

    @GetMapping("/consumer/dept/get/{id}")
    public Dept getById(@PathVariable("id") Long id) {
//        return restTemplate.getForObject(EEST_URL_DEFALUT + "/dept/get/" + id, Dept.class);
        return this.deptClientService.getById(id);
    }
}
